﻿using GN.Pay.TradeChannel.DefaultImpl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.UnionPay.Request
{
    [Serializable]
    class WebPayRequest : APayRequest
    {
        public WebPayRequest(IChannel channel,
        IOrderPayApply order,
        UnionPayConfigure config)
        {
            this.Init(channel, order, config);
        }

        public void Init(IChannel channel,
        IOrderPayApply order,
        UnionPayConfigure config)
        {
            this.merId = config.ConfigureInfo.AccountId;
            this.version = "5.0.0";
            this.encoding = "UTF-8";
            this.signMethod = "01";
            this.txnType = "01";
            this.txnSubType = "01";
            this.bizType = "000201";
            this.channelType = "07";
            this.accessType = "0";
            this.accType = "01";
            this.currencyCode = "156";
            this.frontUrl = config.PayReturnUrl;
            this.backUrl = config.PayNotifyUrl;
            this.orderId = order.OrderNo;
            this.txnTime = order.StartTime.ToString("yyyyMMddHHmmss");
            this.txnAmt = Convert.ToInt64(Math.Round(order.OrderMoney, 2) * 100);
            this.reqReserved = order.ExtraParam;
            this.orderDesc = order.Subject;
            //this.issInsCode = "ICBC";
        }

        public string orderDesc { get; set; }

        /// <summary>
        /// 商户订单号 orderId 必填 商户端生成 
        /// </summary>
        public string orderId { get; set; }

        /// <summary>
        /// 订单发送时间 txnTime 必填 商户发送交易时间  
        /// </summary>
        public string txnTime { get; set; }

        /// <summary>
        /// 交易金额 txnAmt 必填 单位为分 
        /// </summary> 
        public long txnAmt { get; set; }

        /// <summary>
        /// 交易币种 currencyCode 必填 默认为156  
        /// </summary>
        public string currencyCode { get; set; }

        ///// <summary>
        ///// 发卡机构代码
        ///// </summary>
        //public string issInsCode { get; set; }

        /// <summary>
        /// 请求方保留域
        /// </summary>
        public string reqReserved { get; set; }


        /// <summary>
        /// 生成
        /// </summary>
        /// <param name="config">配置</param>
        /// <returns></returns>
        public virtual ITradePayApplyResult Build(ITradeChannel channel,
            UnionPayConfigure config)
        {
            this.txnTime = DateTime.Now.ToString("yyyyMMddHHmmss");
            var param = this.BuildSignUrlLinkParam(channel, config, "signature");
            return new TradePayApplyResult("", config.GateWayUrl, param, TradePayApplyResultCallType.Web_Post);
        }
    }
}
